package edu.usx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.usx.server.mapper.SitesMapper;
import edu.usx.server.pojo.Favorite;
import edu.usx.server.mapper.FavoriteMapper;
import edu.usx.server.pojo.RespBean;
import edu.usx.server.pojo.Sites;
import edu.usx.server.pojo.User;
import edu.usx.server.service.IFavoriteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.usx.server.service.IUserService;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Usx
 * @since 2024-07-23
 */
@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite> implements IFavoriteService {
    @Resource
    private IUserService iUserService;
    @Resource
    private FavoriteMapper favoriteMapper;

    @Resource
    private SitesMapper sitesMapper;
    @Override
    public RespBean getFavoriteList(Principal principal) {
        List <Sites> sites = sitesMapper.getCommonSites();
        if (principal == null) {

            return RespBean.error("用户未登陆",sites);
        }
        else {
            String username= principal.getName();
            //根据用户名获取完整用户对象
            User user = iUserService.getUserByUserName(username);
            int userID = user.getId();
            List<Integer> ids =favoriteMapper.getFavoriteSite(userID);

            for (Integer id : ids) {
                Sites sites1 = sitesMapper.getSitesById(Long.valueOf(id));
                sites.add(sites1);
            }

            return RespBean.success("成功获取",sites);
        }

    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public RespBean addSite(Long id, Principal principal) {
        if (id == null || principal == null) {
            return RespBean.error("无效的参数");
        }
        String username= principal.getName();
        //根据用户名获取完整用户对象
        User user = iUserService.getUserByUserName(username);
        if (user == null) {
            return RespBean.error("用户未登陆");
        }

        Sites sites = sitesMapper.selectById(id);
        if (sites==null) {
            return RespBean.error("该服务不存在");
        }
        if (sites.getStatus()!=1) {
            return RespBean.error("该服务暂时无法收藏");
        }

        if (sites.getCategoryId()==1) {
            return RespBean.error("已收藏此服务");
        }



        List<Favorite>  existingFavorite = favoriteMapper.getSitesByUser(id, user.getId());
        if (existingFavorite.size()==0) {
            Favorite favorite = new Favorite(user.getId(),id, LocalDateTime.now());
            favoriteMapper.insert(favorite);
            return RespBean.success("新增成功");

        }else {
            return RespBean.error("你已经收藏过了");
        }

    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public RespBean deleteFavorite(Long id, Principal principal) {
        if (id == null || principal == null) {
            return RespBean.error("无效的参数");
        }
        String username= principal.getName();
        //根据用户名获取完整用户对象
        User user = iUserService.getUserByUserName(username);
        if (user == null) {
            return RespBean.error("用户未登陆");
        }

        Sites sites = sitesMapper.selectById(id);
        if (sites.getCategoryId()==1) {
            return RespBean.error("不能删除此服务");
        }
        List<Favorite>  existingFavorite = favoriteMapper.getSitesByUser(id, user.getId());
        if (existingFavorite.size()==0) {
            return RespBean.error("未收藏此服务无法删除");

        }else {
            QueryWrapper<Favorite> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", user.getId())
                    .eq("site_id",id);

            int rows = favoriteMapper.delete(wrapper);
            if (rows>0) {
                return RespBean.success("删除成功");
            }
            else {
                return RespBean.success("删除失败");
            }
        }

    }

}
